package com.itheima.sh.esdemo;

import com.alibaba.fastjson.JSON;
import com.itheima.sh.esdemo.pojo.User;
import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.MatchAllQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;

/**

 */
public class ESSearch {

    private RestHighLevelClient client;

    @Before
    public void init() {
        //创建Rest客户端
        client = new RestHighLevelClient(
                RestClient.builder(
                        //如果是集群，则设置多个主机，注意端口是http协议的端口
                        new HttpHost("192.168.200.150", 9200, "http")
                )
        );
    }

    @Test
    public void matchSearch() throws IOException {
        //创建SearchSourceBuilder对象
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        //添加查询条件QueryBuilders
        MatchAllQueryBuilder matchAllQuery = QueryBuilders.matchAllQuery();
        sourceBuilder.query(matchAllQuery);
        //如：添加排序、分页等其它条件
        //创建SearchRequest对象，并制定索引库名称
        SearchRequest request = new SearchRequest("user");
        //添加SearchSourceBuilder对象到SearchRequest对象source中
        request.source(sourceBuilder);
        //发起请求，得到结果
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        //解析结果SearchResponse
        SearchHits searchHits = response.getHits();
        //获取总条数
        long total = searchHits.getTotalHits().value;
        System.out.println("total = " + total);
        //获取SearchHits数组，并遍历
        SearchHit[] hits = searchHits.getHits();
        for (SearchHit hit : hits) {
            //获取分数
            System.out.println(hit.getScore());
            //获取其中的_source是JSON数据
            String json = hit.getSourceAsString();
            //将_source反序列化为User对象
            User user = JSON.parseObject(json, User.class);
            System.out.println(user);

        }

    }

    @Test
    public void termSearch() throws IOException {
        //创建SearchSourceBuilder对象
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        //添加查询条件QueryBuilders.termQuery()
        TermQueryBuilder termQuery = QueryBuilders.termQuery("note", "黑马");
        sourceBuilder.query(termQuery);
        //创建SearchRequest对象，并制定索引库名称
        SearchRequest request = new SearchRequest("user");
        //添加SearchSourceBuilder对象到SearchRequest对象source中
        request.source(sourceBuilder);
        //发起请求，得到结果
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        //解析结果SearchResponse
        SearchHits hits = response.getHits();
        //获取总条数
        System.out.println("总条数:" + hits.getTotalHits().value);
        //获取SearchHits数组，并遍历
        for (SearchHit hit : hits) {
            //获取其中的_source，是JSON数据
            String json = hit.getSourceAsString();
            //把_source反序列化为User对象
            User user = JSON.parseObject(json, User.class);
            System.out.println("user = " + user);
        }

    }
}
